import { Injectable } from "@nestjs/common";
import config from "src/config"
import * as tencentcloud from "tencentcloud-sdk-nodejs"
import { Client as TClient } from "tencentcloud-sdk-nodejs/tencentcloud/services/tms/v20201229/tms_client";
import { Client as IClient } from "tencentcloud-sdk-nodejs/tencentcloud/services/ims/v20201229/ims_client";

const TmsClient = tencentcloud.tms.v20201229.Client
const ImsClient = tencentcloud.ims.v20201229.Client


@Injectable()
export class TmsService {
	tmsClient:TClient
	imsClient:IClient
	constructor() {
		this.tmsClient=new TmsClient({
			region:config.tms.Region,
			credential:{
				secretId: config.tms.SecretId,
				secretKey: config.tms.SecretKey,
			}
		})
		this.imsClient=new ImsClient({
			region:config.tms.Region,
			credential:{
				secretId: config.tms.SecretId,
				secretKey: config.tms.SecretKey,
			}
		})
	}

	/**检测敏感字符 */
	async checkText(text:string){
		let checkTextRes= await this.tmsClient.TextModeration({Content:Buffer.from(text, 'utf8').toString('base64')})
		return checkTextRes.Suggestion=='Pass'
	}
	/**检测敏感图片 */
	async checkImage(url:string){
		let res= await this.imsClient.ImageModeration({FileUrl:url+'?imageMogr2/thumbnail/1000x',BizType:"wallpaper"})
		if(res.Score<80&&res.Suggestion!='Pass'){
			res.Suggestion='Pass'
		}
		return res
	}

}
